<template>
  <view>
    <uni-grid :column="2" v-if="devices.length!=0" :showBorder="false" @change="change" :square="false">
      <uni-grid-item v-for="(item, index) in devices" :index="index" :key="index" @click="back(item)">
        <device-card :device="item" ></device-card>
      </uni-grid-item>
    </uni-grid>
    <o-empty v-if="devices.length==0" ></o-empty>
  </view>
</template>

<script>
import {DeviceService} from "./deviceService";
import DeviceCard from "../../../components/DeviceCard";
import OEmpty from "../../../../uni_modules/o-empty/components/o-empty/o-empty";

export default {
  components: {OEmpty, DeviceCard},
  data() {
    return {
      devices: [],
      type: "",//如果type为select，表示设备可以点击，并返回
    }
  },
  created() {
    this.init();
  },
  onLoad(options) {
    this.type = options.type;
    console.log(this.type)
  },
  methods: {
    init() {
      DeviceService.findDevicesByUser(uni.getStorageSync('userInfo').eid).then((res) => {
        this.devices = res.data;
        console.log("devices", res.data)
      })
    },
    change(e) {
      let {index} = e.detail
      if(this.type!='select'){return}
      this.back(this.devices[index])
    },
    back(item){
      let pages = getCurrentPages(); // 当前页面
      let beforePage = pages[pages.length - 2]; // 上一页
      console.log(beforePage.$vm,item)
      beforePage.$vm.deviceCode = item.deviceCode;
      beforePage.$vm.findDeviceInfo();
      uni.navigateBack({
        delta:1,//返回层数，2则上上页
      })
    }
  }
}
</script>

<style>
.grid-item-box {
  padding: 40 rpx;
}
</style>
